package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.SectionAdapter2;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommentList;
import com.lc.luzhengtang.conn.ConnCommentList;
import com.lc.luzhengtang.divider.SimpleDividerItemDecoration;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class CommentListActivity2 extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private List<CommentList.DataBeanXX.DataBeanX> mData;
    private SectionAdapter2 sectionAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commentlist2);
        ButterKnife.bind(this);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(this));
        mData = new ArrayList<>();
        initData();
        sectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("ordersn", mData.get(position).getOrdersn());
                intent.setClass(CommentListActivity2.this, OrderDetailActivity2.class);
                startActivity(intent);

            }
        });
    }


    protected void initData() {
        rvList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        rvList.setLayoutManager(new LinearLayoutManager(this));

        sectionAdapter = new SectionAdapter2(null);
        sectionAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        sectionAdapter.isFirstOnly(false);
        Drawable drawable = ContextCompat.getDrawable(CommentListActivity2.this, R.drawable.line_divider);
        rvList.addItemDecoration(new SimpleDividerItemDecoration(CommentListActivity2.this, drawable, 1));

        rvList.setAdapter(sectionAdapter);

        swipeLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        swipeLayout.setOnRefreshListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void setRefreshing(final boolean refreshing) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refreshing);
            }
        });
    }

    @Override
    public void onRefresh() {

        new ConnCommentList(BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<CommentList>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
                sectionAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommentList commentList) throws Exception {
                super.onSuccess(toast, type, o, commentList);
             /*   sectionAdapter = new SectionAdapter(commentList.getData());
                rvList.setAdapter(sectionAdapter);*/
                mData = commentList.getData().getData();
                // results.addAll(info.data);
                if (mData != null) {
                    sectionAdapter.setNewData(mData);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);
                dismissLoading();
            }
        }

        ).execute(CommentListActivity2.this, false);

    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
